### -*- coding: utf-8 -*- #############################################
# Разработано компанией Ключевые Решения (http://keysolutions.ru/)
# Все права защищены, 2006-2007
#
# Developed by Key Solutions (http://keysolutions.ru/)
# All right reserved, 2006-2007
#######################################################################
"""SummaryView class for the Zope 3 based diplom.browser package

$Id: geneticview.py 65 2007-12-28 20:27:50Z Arvi3D $
"""
__author__  = "Arvid"
__license__ = "ZPL"
__version__ = "$Revision: 65 $"
__date__ = "$Date: 2007-12-28 22:27:50 +0200 (Fri, 28 Dec 2007) $"

from zope.interface import Interface
from zope.app.i18n import ZopeMessageFactory as _
from zope.app import zapi

from diplom.interfaces import IGenetic, IGenerated

EMPTY_SUM = [(0.0,''), (1.0, '')]

def checkCategory(params):
    """DEPRECATED"""
    coef =  float(params[1][0][1])/params[0][0][1]
    print "coef --> ", coef
    if coef > 2.0:
        return 0
    elif coef < 0.5:
        return 2
    else:
        return 1

def applyDataToCategory(curSummary, data, request):
    eval = IGenetic(data).eval

    if eval > curSummary[0][0]:
        curSummary[0] = (eval, zapi.absoluteURL(data, request))
    if eval < curSummary[1][0]:
        curSummary[1] = (eval, zapi.absoluteURL(data, request))

    print curSummary

class SummaryView(object) :

    def getSummary(self):
        """get sequece or dict with summary data"""
        res = {}
        for data in self.context.values():
            gen = IGenerated(data)
            weight = gen.weight
            params = gen.params

            #TODO: вынести в отдельный класc и сделать настройку на основе рецептов
            if not res.has_key(weight):
                res[weight] = [[(0.0,''), (1.0, '')], [(0.0,''), (1.0, '')], [(0.0,''), (1.0, '')]]

            eval = IGenetic(data).eval

            idx = checkCategory(params)
            print "index --> ", idx
            print "res[weight] --> ", res[weight]
            curSum = res[weight][idx]

            applyDataToCategory(curSum, data, self.request)
            print "curSum --> ", curSum
            print "res[weight] --> ", res[weight]

        res = res.items()
        res.sort()

        return res

